---
title: Building NativeLink with Bazel
description: Instructions on building NativeLink with Bazel.
---

These instructions contain information on how to work with NativeLink for Bazel
users.

If you're using the Nix flake you're all set.

If you're running outside of nix, install [Bazelisk](https://github.com/bazelbuild/bazelisk/tree/master)
manually and make sure you have a recent functional C++ toolchain with LLD as
linker. On Unix you'll also have to add
`--extra_toolchains=@rust_toolchains//:all` to all of your Bazel invocations to
allow the use of non-hermetic Rust toolchains. Windows is non-hermetic by
default.

## Build

The following commands places an executable in `./bazel-bin/nativelink` and
starts the service:

import { Tabs, TabItem } from '@astrojs/starlight/components';

<Tabs syncKey="os">
  <TabItem label="Unix">
    ```sh
    bazel run nativelink -- \
        $(pwd)/nativelink-config/examples/basic_cas.json5
    ```
  </TabItem>
  <TabItem label="Unix without Nix">
    ```sh
    bazel run --extra_toolchains=@rust_toolchains//:all nativelink -- \
        $(pwd)/nativelink-config/examples/basic_cas.json5
    ```
  </TabItem>
  <TabItem label="Windows">
    ```sh
    bazel run --config=windows nativelink -- \
        $(pwd)/nativelink-config/examples/basic_cas.json5
    ```
  </TabItem>
</Tabs>

For optimized builds:

<Tabs syncKey="os">
  <TabItem label="Unix">
    ```sh
    bazel run -c opt nativelink -- \
        $(pwd)/nativelink-config/examples/basic_cas.json5
    ```
  </TabItem>
  <TabItem label="Unix without Nix">
    ```sh
    bazel run -c opt --extra_toolchains=@rust_toolchains//:all nativelink -- \
        $(pwd)/nativelink-config/examples/basic_cas.json5
    ```
  </TabItem>
  <TabItem label="Windows">
    ```sh
    bazel run --config=windows -c opt nativelink -- \
        $(pwd)/nativelink-config/examples/basic_cas.json5
    ```
  </TabItem>
</Tabs>

:::caution
The Rust compiler `rustc` generates large artifacts during compilation,
including dependencies, macros, and intermediate files. When compiling programs
from source, be mindful of the associated files' impact on your disk usage in
the `bazel-bin/` directory.

If you're facing issues due to this, run the following command to clear cache
files:

```sh
bazel clean --expunge
```
:::

## Test

To run tests with Bazel:

<Tabs syncKey="os">
  <TabItem label="Unix">
    ```sh
    bazel test //... --verbose_failures
    ```
  </TabItem>
  <TabItem label="Unix without Nix">
    ```sh
    bazel test --extra_toolchains=@rust_toolchains//:all //... --verbose_failures
    ```
  </TabItem>
  <TabItem label="Windows">
    ```sh
    bazel test --config=windows //... --verbose_failures
    ```
  </TabItem>
</Tabs>
